const dbpool = require('../config/dbpoolconfig');

const groupController = {
    // 群组列表
    groupList(req, res) {
        dbpool.connect('select * from grouplist', [], (err, data) => {
            if (!err) {
                res.send(data);
            }
        })
    },
    // 搜索
    groupListSearch(req, res) {
        let groupId = req.query.groupId || '';
        let groupName = req.query.groupName || '';
        let limitNum = req.query.limitNum || '';
        let addLimit = req.query.addLimit || '';
        let mode = req.query.selectValue || '';
        let msgDate = req.query.msgDate || '';
        let createDate = req.query.createDate || '';



        let sql = 'select * from grouplist where 1=1 ';
        let params = [];

        if (groupId.trim() != '') {
            sql += 'and groupId  like ? ';
            groupId = '%' + groupId.trim() + '%';
            params.push(groupId)
        }

        if (groupName.trim() != '') {
            sql += 'and groupName like ? ';
            groupName = '%' + groupName.trim() + '%';
            params.push(groupName)
        }

        if (limitNum != '' && limitNum != '全部') {
            let range = limitNum.split('-');
            sql += 'and limitNum between ? and ? ';
            params.push(range[0], range[1])
        }
        if (addLimit != '' && addLimit != '全部') {
            sql += 'and addLimit =  ? ';
            params.push(addLimit)

        }

        if (mode != '' && mode != '全部') {
            sql += 'and mode =  ? ';
            params.push(mode)

        }

        if (msgDate != '') {
            sql += 'and msgDate between ? and ? ';
            params.push(msgDate[0], msgDate[1])

        }

        if (createDate != '') {
            sql += 'and createDate between ? and ? ';
            params.push(createDate[0], createDate[1])

        }

        console.log(sql);
        console.log(params);

        dbpool.connect(sql, params, (err, data) => {
            if (!err) {
                res.send(data);
            }
        })
    },
    // 批量删除
    delSelectionGroup(req, res) {
        
        let delSelection = req.query.delSelectionData || '';
        let sql = 'delete from grouplist where groupId = ? ';
        let params = [];
        console.log(delSelection);
        if (delSelection != '' || delSelection != []) {
            if (delSelection.length > 1) {
                for (let i = 1; i < delSelection.length; i++) {
                    sql += 'or groupId = ? '
                }
                delSelection.forEach(item => {
                    params.push(item)
                })
            } else {
                delSelection.forEach(item => {
                    params.push(item)
                })
            }

            dbpool.connect(sql, params, (err, data) => {
                if (!err) {
                    res.send('success')
                }
            })
        }else {
            res.send('null')
        }
    },
    // 修改状态
    groupListMode(req, res) {
        let groupId = req.query.groupId || req.body.groupId;
        let mode = req.query.mode || req.body.mode;
        let operate = req.query.operate || req.body.operate;

        let params = []

        if (mode == '正常') {
            mode = '已冻结'
            operate = '解冻'
            params.push(mode, operate, groupId)
        } else if(mode == '已冻结') {
            mode = '正常'
            operate = '冻结'
            params.push(mode, operate, groupId)
        }

        dbpool.connect('UPDATE grouplist SET mode = ?,operate=? WHERE groupId = ?', params, (err, data) => {
            if (!err) {
                dbpool.connect('update grouprecord INNER JOIN grouplist ON grouplist.groupId = grouprecord.reportedGroupId set grouprecord.reportedGroupMode = grouplist.mode ,grouprecord.operate = grouplist.operate', [], (err, data) => {
                    if (!err) {
                        res.send('success')
                    }
                })
            }
        })
    },
    // 解散
    disbandGroupList(req,res){
        let groupId = req.query.groupId || req.body.groupId;
        let mode = req.query.mode || req.body.mode;
        let operate = req.query.operate || req.body.operate;
        let disband = req.query.disband || req.body.disband;

        let params = []

        if (mode != '已解散') {
            mode = '已解散'
            operate = ''
            disband = ''
            params.push(mode, operate,disband, groupId)
        } 

        dbpool.connect('UPDATE grouplist SET mode = ?,operate=?,disband=? WHERE groupId = ?', params, (err, data) => {
            if (!err) {
                dbpool.connect('update grouprecord INNER JOIN grouplist ON grouplist.groupId = grouprecord.reportedGroupId set grouprecord.reportedGroupMode = grouplist.mode ,grouprecord.operate = grouplist.operate,grouprecord.disband = grouplist.disband', [], (err, data) => {
                    if (!err) {
                        res.send('success')
                    }
                })
            }
        })
    },

    // 举报记录
    groupRecordList(req,res){
        dbpool.connect('select * from grouprecord', [], (err, data) => {
            if (!err) {
                res.send(data);
            }
        })
    },
    // 搜索
    groupRecordListSearch(req,res){
        console.log(1111111);
        let reportID= req.query.reportID || '';
        let reportName= req.query.reportName || '';
        let reportedGroupID= req.query.reportedGroupID || '';
        let reportedGroupName= req.query.reportedGroupName || '';
        let reportedContent= req.query.reportedContent || '';
        let reportedGroupMode= req.query.reportedGroupMode || '';
        let reportDate= req.query.reportDate || '';

        let sql = 'select * from grouprecord where 1=1 '
        let params = [];

        if(reportID.trim() != ''){
            sql += 'and reportID like ? ',
            reportID = '%'+reportID.trim()+'%'
            params.push(reportID)
        }
        if(reportName.trim() != ''){
            sql += 'and reportName like ? ',
            reportName = '%'+reportName.trim()+'%'
            params.push(reportName)
        }
        if(reportedGroupID.trim() != ''){
            sql += 'and reportedGroupId like ? ',
            reportedGroupID = '%'+reportedGroupID.trim()+'%'
            params.push(reportedGroupID)
        }
        if(reportedGroupName.trim() != ''){
            sql += 'and reportedGroupName like ? ',
            reportedGroupName = '%'+reportedGroupName.trim()+'%'
            params.push(reportedGroupName)
        }
        if(reportedContent != '' && reportedContent != '全部'){
            sql += 'and reportedContent = ? ',
            params.push(reportedContent)
        }
        if(reportedGroupMode != '' && reportedGroupMode != '全部'){
            sql += 'and reportedGroupMode = ? ',
            params.push(reportedGroupMode)
        }
        if(reportDate != ''){
            sql += 'and reportDate between ? and ? ',
            params.push(reportDate[0],reportDate[1])
        }

        dbpool.connect(sql,params,(err,data)=>{
            if(!err){
                res.send(data)
            }
        })
    },
    // 改变群状态
    groupRecordListMode(req,res){
        let reportedGroupId = req.query.reportedGroupId || req.body.reportedGroupId;
        let reportedGroupMode = req.query.reportedGroupMode || req.body.reportedGroupMode;
        let operate = req.query.operate || req.body.operate;

        let params = []


        if (reportedGroupMode == '正常') {
            reportedGroupMode = '已冻结'
            operate = '解冻'
            params.push(reportedGroupMode, operate, reportedGroupId)
        } else if(reportedGroupMode == '已冻结'){
            reportedGroupMode = '正常'
            operate = '冻结'
            params.push(reportedGroupMode, operate, reportedGroupId)
        }

        dbpool.connect('UPDATE grouprecord SET reportedGroupMode = ?,operate=? WHERE reportedGroupId = ?', params, (err, data) => {
            if (!err) {
                dbpool.connect('update grouplist INNER JOIN grouprecord ON grouplist.groupId = grouprecord.reportedGroupId set grouplist.mode = grouprecord.reportedGroupMode,grouplist.operate = grouprecord.operate',[],(err,data)=>{
                    if(!err){
                        res.send('success')
                    }
                })
            }
        })
    },
    disbandGroupRecordList(req,res){
        let reportedGroupId = req.query.reportedGroupId || req.body.reportedGroupId;
        let reportedGroupMode = req.query.reportedGroupMode || req.body.reportedGroupMode;
        let operate = req.query.operate || req.body.operate;
        let disband = req.query.disband || req.body.disband;

        let params = []

        if (reportedGroupMode != '已解散') {
            reportedGroupMode = '已解散'
            operate = ''
            disband = ''
            params.push(reportedGroupMode, operate,disband, reportedGroupId)
        } 

        dbpool.connect('UPDATE grouprecord SET reportedGroupMode = ?,operate=?,disband=? WHERE reportedGroupId = ?', params, (err, data) => {
            if (!err) {
                dbpool.connect('update grouplist INNER JOIN grouprecord ON grouplist.groupId = grouprecord.reportedGroupId set grouplist.mode =  grouprecord.reportedGroupMode,grouplist.operate = grouprecord.operate,grouplist.disband = grouprecord.disband', [], (err, data) => {
                    if (!err) {
                        res.send('success')
                    }
                })
            }
        })
    }

}
module.exports = groupController;